package com.sxjh.controller.ny;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.annotation.UserLog;
import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.common.ResultException;
import com.sxjh.common.ZPage;
import com.sxjh.entity.DeiverCarrierEntity;
import com.sxjh.entity.WagonNumber;
import com.sxjh.entity.param.CarrierListParam;
import com.sxjh.enums.DelFlagEnum;
import com.sxjh.service.DeiverCarrierService;
import com.sxjh.service.WagonNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 内部倒运--司机所属承运商 前端控制器
 * </p>
 *
 * @author guanxy
 * @since 2023-05-29
 */
@Api(tags = "司机所属承运商")
@RestController
@RequestMapping("/carrier")
public class DeiverCarrierController extends BaseController {


    @Autowired
    private DeiverCarrierService deiverCarrierService;

    @Autowired
    private WagonNumberService wagonNumberService;


    @ApiOperation("分页列表")
    @GetMapping(value = "/carrier.list")
    public Result<ZPage<DeiverCarrierEntity>> list(CarrierListParam param) {
        LambdaQueryWrapper<DeiverCarrierEntity> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(param.getName())) {
            queryWrapper.like(DeiverCarrierEntity::getName, param.getName());
        }
        if (StrUtil.isNotBlank(param.getContactName())) {
            queryWrapper.like(DeiverCarrierEntity::getContactName, param.getContactName());
        }
        if (StrUtil.isNotBlank(param.getContactPhone())) {
            queryWrapper.like(DeiverCarrierEntity::getContactPhone, param.getContactPhone());
        }
        queryWrapper.eq(DeiverCarrierEntity::getDelFlag, DelFlagEnum.NO.getType());
        queryWrapper.orderByDesc(DeiverCarrierEntity::getId);
        Page<DeiverCarrierEntity> aPage = deiverCarrierService.page(new Page<>(param.getCurrent(), param.getSize()), queryWrapper);
        ZPage<DeiverCarrierEntity> zPage = copyPage(aPage, DeiverCarrierEntity::new);
        return Result.ok(zPage);
    }

    @ApiOperation("查询")
    @GetMapping(value = "/{id}")
    public Result<Object> getById(@PathVariable("id") String id) {
        DeiverCarrierEntity carrierEntity = deiverCarrierService.getById(id);
        return Result.ok(carrierEntity);
    }

    @ApiOperation("新增")
    @PostMapping(value = "/add")
    @UserLog(desc = "内部倒运-新增司机所属承运商", roleCode = "nbdy.carrier.add")
    public Result<Object> create(@RequestBody DeiverCarrierEntity params) throws ResultException {
//        String contactPhone = params.getContactPhone();
//        if (StrUtil.isNotBlank(contactPhone)) {
//            String errMessage = DriverInfosController.checkMobileNumber(contactPhone);
//            if (StrUtil.isNotBlank(errMessage)) return Result.err(errMessage);
//        } else {
//            return Result.err("手机号码不能为空");
//        }
        if (StrUtil.isBlank(params.getContactPhone())) return Result.err("联系方式不能为空");

        if (StrUtil.isNotBlank(params.getName())) {
            LambdaQueryWrapper<DeiverCarrierEntity> query1 = new LambdaQueryWrapper<>();
            query1.eq(DeiverCarrierEntity::getName, params.getName());
            query1.eq(DeiverCarrierEntity::getDelFlag, DelFlagEnum.NO.getType());
            List<DeiverCarrierEntity> list = deiverCarrierService.list(query1);
            if (CollUtil.isNotEmpty(list)) {
                throw new ResultException(params.getName() + "已存在");
            }
        }
        deiverCarrierService.save(params);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @GetMapping(value = "/delete/{id}")
    @UserLog(desc = "内部倒运-删除司机所属承运商", roleCode = "nbdy.carrier.del")
    public Result<Object> delete(@PathVariable("id") String id) {
        int count = wagonNumberService.count(new LambdaQueryWrapper<WagonNumber>().eq(WagonNumber::getUnitId, id));
        if (count > 0) return Result.err("该承运单位在车辆管理中被使用了!");
        DeiverCarrierEntity params = new DeiverCarrierEntity();
        params.setId(Integer.valueOf(id));
        params.setDelFlag(DelFlagEnum.YES.getType());
        deiverCarrierService.updateById(params);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update")
    @UserLog(desc = "内部倒运-修改司机所属承运商", roleCode = "nbdy.carrier.update")
    public Result<Object> delete(@RequestBody DeiverCarrierEntity params) throws ResultException {
        if (StrUtil.isNotBlank(params.getName())) {
            LambdaQueryWrapper<DeiverCarrierEntity> query1 = new LambdaQueryWrapper<>();
            query1.eq(DeiverCarrierEntity::getName, params.getName());
            query1.eq(DeiverCarrierEntity::getDelFlag, DelFlagEnum.NO.getType());
            query1.ne(DeiverCarrierEntity::getId, params.getId());
            List<DeiverCarrierEntity> list = deiverCarrierService.list(query1);
            if (CollUtil.isNotEmpty(list)) {
                throw new ResultException(params.getName() + "已存在");
            }
        }
        deiverCarrierService.updateById(params);
        return Result.ok(HttpStatus.OK);
    }
}
